package com.cch.demo.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cch.demo.bus.domain.Goods;
import com.cch.demo.bus.domain.Salesback;
import com.cch.demo.bus.domain.Customer;
import com.cch.demo.bus.service.GoodsService;
import com.cch.demo.bus.service.SalesbackService;
import com.cch.demo.bus.service.CustomerService;
import com.cch.demo.bus.vo.SalesbackVo;
import com.cch.demo.sys.util.DataGridView;
import com.cch.demo.sys.util.ResultObj;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 华哥
 * @since 2020-03-12
 */
@RestController
@RequestMapping("/salesback")
public class SalesbackController {

    @Resource
    private SalesbackService salesbackServiceImpl;

    @Resource
    private GoodsService goodsServiceImpl;
    @Resource
    private CustomerService customerServiceImpl;

    @RequestMapping("/loadAllSalesback")
    public DataGridView loadAllSalesback(SalesbackVo salesbackVo) {
        IPage<Salesback> page = new Page<>(salesbackVo.getPage(), salesbackVo.getLimit());
        QueryWrapper<Salesback> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(salesbackVo.getCustomerid()!=null&&salesbackVo.getCustomerid()!=0,"customerid",salesbackVo.getCustomerid());
        queryWrapper.eq(salesbackVo.getGoodsid()!=null&&salesbackVo.getGoodsid()!=0,"goodsid",salesbackVo.getGoodsid());
        queryWrapper.ge(salesbackVo.getStartTime()!=null, "salesbacktime", salesbackVo.getStartTime());
        queryWrapper.le(salesbackVo.getEndTime()!=null, "salesbacktime", salesbackVo.getEndTime());
        queryWrapper.like(StringUtils.isNotBlank(salesbackVo.getOperateperson()), "operateperson", salesbackVo.getOperateperson());
        queryWrapper.like(StringUtils.isNotBlank(salesbackVo.getRemark()), "remark", salesbackVo.getRemark());
        queryWrapper.orderByDesc("salesbacktime");
        salesbackServiceImpl.page(page, queryWrapper);
        List<Salesback> records = page.getRecords();
        for (Salesback salesback : records) {
            Customer customer =customerServiceImpl.getById(salesback.getCustomerid());
            if(null!=customer) {
                salesback.setCustomername(customer.getCustomername());
            }
            Goods goods = goodsServiceImpl.getById(salesback.getGoodsid());
            if(null!=goods) {
                salesback.setGoodsname(goods.getGoodsname());
                salesback.setSize(goods.getSize());
            }
        }
        return new DataGridView(page.getTotal(), records);
    }

    @RequestMapping("addSalesback")
    public ResultObj addSalesback(Integer id, Integer number, String remark){
        try{
            salesbackServiceImpl.addSalesbck(id,number,remark);
            return ResultObj.OPERATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.OPERATE_ERROR;
        }
    }
}

